import pyautogui as pg
from time import sleep
import random
# from os import exit()
def start(number,duration):
    step_pixel=36
    name_xuexiqiangguo_win='学习强国'
    for x in pg.getAllTitles():
        if name_xuexiqiangguo_win in x:
            name_xuexiqiangguo_win=x
    xuexi = pg.getWindowsWithTitle(name_xuexiqiangguo_win)
    xuexi[0].activate()

    pg.vscroll(clicks=1100) #确保从页面的第一条开始

    for i in range(number):
        xuexi[0].activate()
        sleep(2)
        pg.click(x=436, y=573)
        sleep(5)
        pg.vscroll(clicks=-500)
        sleep(duration+random.randint(3,17))#修改变动时间，波动在+3到+17秒
        pg.hotkey('ctrl','f4')
        sleep(2)
        pg.vscroll(clicks=-step_pixel)


def main():
    # select=pg.confirm(text='请选择功能。通过键盘方向键和回车键操作，勿动鼠标',title='学习强国辅助',buttons=['阅读文章','观看视频'])
    # select=input('请选择功能。通过键盘方向键和回车键操作，勿动鼠标\n1.阅读文章 2.观看视频\n你的选择：')
    num_text = input("请输入文章的阅读数量：\n")
    dur_text = input("请输入每篇文章的阅读时长（秒）：\n")
    start(number=int(num_text),duration=int(dur_text))
    # if select=='阅读文章':
    # if select=='1':
    #    start(20,50)#用于看文章
    # elif select=='观看视频':
    # elif select=='2':
    #    start(10,120)#用于看视频
    # else:
    #    print('输入错误，只能输入“1”或“2”！')
    # pg.alert(text='已结束')
    name_study='study_strong'
    for x in pg.getAllTitles():
        if name_study in x:
            name_study=x
    win_run = pg.getWindowsWithTitle(name_study)
    win_run[0].activate()#运行结束后，将本软件至于最前

    input('已结束')
if __name__ == '__main__':
    main()
    # print(random.randint(3, 27))
    # select=pg.confirm(text='请选择功能。通过键盘方向键和回车键操作，勿动鼠标',title='学习强国辅助',buttons=['阅读文章','观看视频'])
    # print(select)
    # print(pg.position())